<!--
  -    Copyright (c) 2018-2025, hycloud All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: hycloud
  -->
<template>
    <div class="execution">
        <basic-container>
            <avue-crud ref="crud"
                       :page.sync="page"
                       :data="tableData"
                       :permission="permissionList"
                       :table-loading="tableLoading"
                       :option="tableOption"
                       @on-load="getList"
                       @search-change="searchChange"
                       @refresh-change="refreshChange"
                       @size-change="sizeChange"
                       @current-change="currentChange"
                       @row-update="handleUpdate"
                       @row-save="handleSave"
                       @row-del="rowDel">
                       <template
                            slot="useDates"
                            slot-scope="scope">
                            {{ scope.row.startDate }} - {{ scope.row.endDate }}
                        </template>
                        <template slot="menuLeft">
                            <el-button
                            icon="el-icon-circle-plus"
                            size="small"
                            type="primary"
                            @click="addNodeform('office_bill_apply')">办公用房申请</el-button>
                        </template>
                        <template slot="menu" slot-scope="scope">
                            <el-button type="text"
                            size="small"
                            @click="goDetail(scope)">详情</el-button>
                            <el-button type="text"
                            size="small"
                            v-if="scope.row.bizStatus === '2'"
                            @click="leave(scope, 2)">交房</el-button>
                            <el-button type="text"
                            size="small"
                            v-if="scope.row.bizStatus === '1'"
                            @click="leave(scope, 1)">退房</el-button>
                        </template>
            </avue-crud>
        </basic-container>
        <el-dialog
            :visible.sync="modal"
            v-if="modal"
            title=""
            width="1000px"
            top="2vh"
            :close-on-click-modal="false"
            append-to-body>
            <div class="form-designer">
                <render-form
                   :option="option" :formObj="formObj" @getModal="getModal"
                ></render-form>
            </div>
        </el-dialog>
         <el-dialog
            :visible.sync="modal4"
            v-if="modal4"
            :title="title"
            width="500px"
            top="40vh"
            :close-on-click-modal="false"
            append-to-body>
            <div class="return-block">
                <el-row style="line-height: 40px">
                    <el-col :span="6">{{label}}</el-col>
                    <el-col :span="18">
                        <el-date-picker
                            v-model="params.checkDate"
                            type="date"
                            :placeholder="'请选择' + label"
                            value-format="yyyy-MM-dd">
                        </el-date-picker>
                    </el-col>
                </el-row>
                <el-row style="line-height: 40px">
                    <el-col :span="6">备注：</el-col>
                    <el-col :span="18">
                        <el-input type="textarea" v-model="params.remark" :rows="4" :maxlength="512"></el-input>
                    </el-col>
                </el-row>
            </div>
            <div class="btn-footer">
                <el-button type="primary" @click="leaveSubmit">确 定</el-button>
                <el-button @click="close">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {fetchList, checkIn, checkOut, addObj, putObj, delObj} from '@/api/property/oapropertyofficebill'
    import RenderForm from '@/components/table-design/newRender'
    import ViewForm from '@/components/table-design/viewRender'
    import addView from '@/mixins/addView'
    import { remote } from "@/api/admin/dict";
    import {tableOption} from '@/const/crud/property/oapropertyofficebill'
    import {mapGetters} from 'vuex'

    export default {
        components: { RenderForm, ViewForm },
        mixins: [addView],
        name: 'oapropertyofficebill',
        data() {
            return {
                params: {
                    bizId: '',
                    officeId: '',
                    checkDate:'',
                    remark: ''
                },
                modal4: false,
                searchForm: {},
                tableData: [],
                searchForm: {},
                tableData: [],
                tableData1: [],
                tableData2: [],
                option: {},
                optionsHouseType: [],
                optionRylb: [],
                optionsZw: [],
                option1: {},
                modal: false,
                modal1: false,
                form: {},
                form1: {},
                assetIds: [],
                title: '',
                label: '',
                form2: {},
                formObj: {
                    code: 'office_bill_apply',
                    title: '',
                    flag: 'start',
                    bizData: {}
                },
                page: {
                    total: 0, // 总页数
                    currentPage: 1, // 当前页数
                    pageSize: 20 // 每页显示多少条
                },
                tableLoading: false,
                tableOption: tableOption
            }
        },
        computed: {
            ...mapGetters(['permissions', 'tagList']),
            permissionList() {
                return {
                    addBtn: false,
                    delBtn: false,
                    editBtn: false
                };
            }
        },
        methods: {
            leave(scope, type) {
                this.modal4 = true
                if (type === 2) {
                    this.title = '交房'
                    this.label = '交付时间：'
                } else {
                    this.title = '退房'
                    this.label = '退房时间：'
                }
                this.params.bizId = scope.row.bizId
                this.params.officeId = scope.row.officeId
            },
            close() {
                this.modal4 = false
                this.params = {
                    officeId: '',
                    bizId: '',
                    remark: '',
                    checkDate:''
                }
            },
            leaveSubmit() {
                let _this = this
                 this.$confirm('是否确认提交?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function () {
                    if (_this.title === '交房') {
                        checkIn(_this.params).then(response => {
                            _this.$message.success('交房成功')
                            _this.close()
                            _this.getList(_this.page)
                        })
                    } else {
                        checkOut(_this.params).then(response => {
                            _this.$message.success('退房成功')
                            _this.close()
                            _this.getList(_this.page)
                        })
                    }
                })
            },
            init() {
                remote('personnel_type').then(response => {
                    this.optionRylb = response.data.data
                })
                remote('housing_type').then(response => {
                    this.optionsHouseType = response.data.data
                })
                fetchList2({current: 1, size:1000}).then(response => {
                    this.optionsZw = response.data.data.records
                })
            },
            close1() {
                this.modal1 = false
            },
            handleSelectionChange (val) {
                let list = []
                val.map(item => {
                    list.push(item.assetId)
                })
                this.assetIds = list
            },
            goDetail(scope) {
                let tag = this.tagList.filter(item => {
                    return item.value.indexOf('/db/detail') !== -1
                })
                if (tag.length > 0) {
                    this.$router.$avueRouter.closeTag(tag[0])
                }
                this.$router.push({
                    name: '流程详情',
                    params: {
                        id: scope.row.billId,
                        newtitle: '流程详情-' + scope.row.flowName
                    }
                })
            },
            getZwName(id) {
                let list = this.optionsZw.filter(item => {
                    return item.id === id
                })
                return list.length ? list[0].name : ''
            },
            getReTypeName(id) {
                let list = this.optionRylb.filter(item => {
                    return item.value === id
                })
                return list.length ? list[0].label : ''
            },
            getHouseTypeName(id) {
                let list = this.optionsHouseType.filter(item => {
                    return item.value === id
                })
                return list.length ? list[0].label : ''
            },
            getModal(val) {
                this.modal = val
                this.getList(this.page)
            },
            getList(page, params) {
                this.tableLoading = true
                if (this.searchForm.dateRange) {
                    this.searchForm.startDate = this.searchForm.dateRange[0];
                    this.searchForm.endDate = this.searchForm.dateRange[1];
                }
                fetchList(Object.assign({
                    current: page.currentPage,
                    size: page.pageSize
                }, params, this.searchForm )).then(response => {
                    this.tableData = response.data.data.records
                    this.page.total = response.data.data.total
                    this.tableLoading = false
                }).catch(() => {
                    this.tableLoading=false
                })
            },
            rowDel: function (row, index) {
                this.$confirm('是否确认删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(function () {
                    return delObj(row.bizId)
                }).then(data => {
                    this.$message.success('删除成功')
                    this.getList(this.page)
                })
            },
            handleUpdate: function (row, index, done,loading) {
                putObj(row).then(data => {
                    this.$message.success('修改成功')
                    done()
                    this.getList(this.page)
                }).catch(() => {
                    loading();
                });
            },
            handleSave: function (row, done,loading) {
                addObj(row).then(data => {
                    this.$message.success('添加成功')
                    done()
                    this.getList(this.page)
                }).catch(() => {
                    loading();
                });
            },
            sizeChange(pageSize){
                this.page.pageSize = pageSize
            },
            currentChange(current){
                this.page.currentPage = current
            },
            searchChange(form, done) {
                this.searchForm = form
                this.page.currentPage = 1
                this.getList(this.page, form)
                done()
            },
            refreshChange() {
                this.getList(this.page)
            }
        }
    }
</script>
<style scoped  lang="scss">
/deep/.el-select, .el-input-number, .el-date-editor{
    width: 100%;
}
.btn-footer{
    text-align: center;
}
</style>